﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Framework.Infrastructure;
using Microsoft.Practices.Unity;
using Framework.Config;

namespace Framework.Sample.WebTest
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            try
            {
                Configuration.Instance
                             //.AsyncEventDispatcherBuild()  // build for event subscriber 
                             //.NServieBusBuild()
                             .DefaultCommandBusBuild()
                             .DefaultEventBusBuild()
                             .RegisterMvc()
                             .MvcIgnoreResouceRoute(RouteTable.Routes)
                             ;
            }
            catch (TypeInitializationException ex)
            {

                if (ex.InnerException is System.IO.FileLoadException)
                {
                    var typeLoadException = ex.InnerException as System.IO.FileLoadException;
                    var loaderExceptions = typeLoadException.FusionLog;
                    var str = string.Empty;
                    //loaderExceptions.ForEach(le => str += le.Message + " " + le.Data.ToJson());
                    Console.WriteLine(str);
                }
            }
            
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}